package com.link.base.base.performance.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.link.base.base.approval.model.Flow;
import com.link.base.base.approval.service.FlowService;
import com.link.base.base.assessmentindex.model.AssessmentIndex;
import com.link.base.base.assessmentindex.service.AssessmentIndexService;
import com.link.base.base.performance.dao.mybatis.mapper.PerformanceMapper;
import com.link.base.base.performance.model.Performance;
import com.link.base.base.performance.model.ScoringDetails;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 绩效考核
 *
 * @author 王昊
 * @date 2020-08-26 17:46:09
 */
@Service
public class PerformanceServiceImpl extends BasicServiceImpl<Performance> implements PerformanceService {

    @Resource
    private PerformanceMapper    performanceMapper;

    @Resource
    private ScoringDetailsService scoringDetailsService;

    @Resource
    private AssessmentIndexService assessmentIndexService;

    @Resource
    private FlowService flowService;


    @Override
    public BasicMapper<Performance> getBasicMapper() {
        return performanceMapper;
    }


    /**
     * 新建绩效考核自评，点击保存按钮向考核打分明细表插入考核指标
     * @author 王昊
     * @date 11:13 2021/3/22
     * @param entity
     * @return void
     **/
    @Override
    public void beforInsert(Performance entity) throws Exception {
        super.beforInsert(entity);
        //20210413逻辑修改
        //绩效考核头选择员工，根据员工的岗位id去指标维护表查询对应岗位指标，依次插入到打分明细表 (弃用)
        //20210510逻辑修改
        //绩效考核头选择员工，值列表选择员工岗位，根据员工岗位去指标表查询对应指标（有效且岗位名称正确），依次插入打分明细表
        AssessmentIndex queryAsse = new AssessmentIndex();
        queryAsse.setUserPostn("Y");
        //获取绩效考核头岗位值列表
        queryAsse.setPostnName(entity.getPosition());
        List<AssessmentIndex> assessmentIndices = assessmentIndexService.queryAll(queryAsse);
        if (assessmentIndices.isEmpty() || assessmentIndices == null) {
            //请联系管理员维护考核指标！
            throw new ServiceException("IYUPERFORM-004");
        }
        for (AssessmentIndex assessmentIndex : assessmentIndices) {
            ScoringDetails scoringDetails = new ScoringDetails();
            scoringDetails.setPerformanceId(entity.getId());
            scoringDetails.setIndexId(assessmentIndex.getId());
            //考核指标
            scoringDetails.setAssessmentIndex(assessmentIndex.getAssessmentIndex());
            //权重占比
            scoringDetails.setKeyPoints(assessmentIndex.getKeyPoints());
            scoringDetailsService.insert(scoringDetails);
        }

        //20210322逻辑修改
        //直接去指标维护表查询所有有效记录，绩效考核头保存时，插入到考核打分明细表
//        AssessmentIndex queryAsse = new AssessmentIndex();
//        List<AssessmentIndex> assessmentIndices = assessmentIndexService.queryAll(queryAsse);
//        if (assessmentIndices.isEmpty() || assessmentIndices == null) {
//            //请联系管理员维护考核指标！
//            throw new ServiceException("IYUPERFORM-004");
//        }
//        for (AssessmentIndex assessmentIndex : assessmentIndices) {
//            ScoringDetails scoringDetails = new ScoringDetails();
//            scoringDetails.setPerformanceId(entity.getId());
//            scoringDetails.setIndexId(assessmentIndex.getId());
//            scoringDetails.setAssessmentItems(assessmentIndex.getAssessmentItems());
//            scoringDetails.setKeyPoints(assessmentIndex.getKeyPoints());
//            scoringDetailsService.insert(scoringDetails);
//        }


        //1.直接在指标维护表查询所有信息 关联中间表 where 中间表的职位id为当前用户id
//        AssessmentIndex asse = new AssessmentIndex();
//        asse.setEmpPostnId(entity.getPostnId());
//        List<AssessmentIndex> assessmentIndex = assessmentIndexService.queryByPostnId(asse);
        //2.将查询到的指标项集合遍历，取指标维护表对应指标信息插入到打分明细表
//        for (AssessmentIndex assessmentIndex1 : assessmentIndex) {
//            ScoringDetails scoringDetails = new ScoringDetails();
//            scoringDetails.setPerformanceId(entity.getId());
//            scoringDetails.setIndexId(assessmentIndex1.getId());
//            scoringDetails.setAssessmentItems(assessmentIndex1.getAssessmentItems());
//            scoringDetails.setAssessmentIndex(assessmentIndex1.getAssessmentIndex());
//            scoringDetails.setKeyPoints(assessmentIndex1.getKeyPoints());
//            scoringDetailsService.insert(scoringDetails);
//        }
        //3.根据用户ID去指标维护表查询是否有特殊指标，有则插入打分明细表
//        AssessmentIndex ass2 = new AssessmentIndex();
//        ass2.setUserId(entity.getUserId());
//        List<AssessmentIndex> assessmentIndices2 = assessmentIndexService.queryByUserId(ass2);
        //处理数据，插入到考核打分明细表
//        for (AssessmentIndex assessmentIndex2 : assessmentIndices2) {
//            ScoringDetails scoringDetails2 = new ScoringDetails();
//            scoringDetails2.setPerformanceId(entity.getId());
//            scoringDetails2.setIndexId(assessmentIndex2.getId());
//            scoringDetails2.setAssessmentItems(assessmentIndex2.getAssessmentItems());
//            scoringDetails2.setAssessmentIndex(assessmentIndex2.getAssessmentIndex());
//            scoringDetails2.setKeyPoints(assessmentIndex2.getKeyPoints());
//            scoringDetailsService.insert(scoringDetails2);
//        }
    }



    /**
     * 删除记录前校验当前行审批状态，若非新建状态，不允许删除
     * 删除绩效考核，级联删除对应自评记录
     * @param entity
     * @throws Exception
     */
    @Override
    public void beforDelete(Performance entity) throws Exception {
        super.beforDelete(entity);
        //删除记录前校验当前行审批状态，若非新建状态，不允许删除
        if (!entity.getApprovalStatus().equals("Created")) {
            throw new ServiceException("IYUPERFORM-001");
        }

        //删除绩效考核，级联删除对应自评记录
        //1.根据绩效考核id查询自评记录
        ScoringDetails sco = new ScoringDetails();
        sco.setPerformanceId(entity.getId());
        scoringDetailsService.deleteByPerfoId(sco);
    }


    /**
     * 绩效自评审批
     * @param entity
     * @throws Exception
     */
    @Override
    public void performSubmit(Performance entity) throws Exception {
        Performance perform = performanceMapper.queryById(entity);

        String approvalType = "PerformSubmit";
        ObjectMapper mapper = new ObjectMapper();
        String approvalDetail = mapper.writeValueAsString(perform);
        //创建审批流程
        flowService.createFlow(perform.getId(), approvalType, null, perform.getUserName(),perform.getId(), approvalDetail);
        //提交审批后，审批状态为【审批中】
        perform.setApprovalStatus("UnderApproval");
        performanceMapper.update(perform);
    }

    /**
     * 审批拒绝与通过方法
     * @param flow
     * @param arg1
     * @throws Exception
     */
    @Override
    public void apprPerformance(Flow flow, String arg1, String arg2, String arg3, String arg4, String arg5) throws Exception {
        Long performanceId = flow.getFlowObjId();
        Performance performance = new Performance();
        performance.setId(performanceId);
        performance.setApprovalStatus(arg1);
        performanceMapper.updateStatus(performance);
    }
}
